<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="仓库名称" prop="warehouseId">
        <el-select clearable v-model="queryParams.warehouseId" placeholder="请选择仓库">
          <el-option v-for="item in houseList" :key="item.id" :label="item.warehouse" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="物资名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入物资名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入申请人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出库时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd('0')"
          v-hasPermi="['park:maInOutRecord:add']"
          >出库</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['park:maInOutRecord:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['park:maInOutRecord:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="maInOutRecordList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="出库编号" align="center" prop="recordNumber" />
      <el-table-column label="出库时间" align="center" prop="operateTime" />
      <!--<el-table-column label="出库部门/人" align="center" prop="deptName">-->
        <!--<template slot-scope="scope">-->
          <!--<span>{{scope.row.deptName}}/{{scope.row.userName}}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column label="出库申请人" align="center" prop="userName" />
      <el-table-column label="出库仓库" align="center" prop="warehouse" >
        <template slot-scope="scope">
          <span>{{scope.row.warehouse}}</span>
        </template>
      </el-table-column>
      <el-table-column label="用途" align="center" prop="use1" />
      <el-table-column label="用于" align="center" prop="usePark" />
      <el-table-column label="物资名称" align="center" prop="materialName" width="180"/>
      <el-table-column label="规格型号" align="center" prop="standards" />
      <el-table-column label="物料编码" align="center" prop="materialNum" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="数量" align="center" prop="quantity"/>
      <el-table-column label="类别" align="center" prop="categoryName" />
      <el-table-column label="单价（元）" align="center" prop="unitPrice" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row,'0')"
            v-hasPermi="['park:maInOutRecord:edit']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['park:maInOutRecord:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listmaInOutRecord,
  getmaInOutRecord,
  delmaInOutRecord,
  addmaInOutRecord,
  updatemaInOutRecord,
} from "@/api/smartParks/property/propertyItems";
import { listData } from "@/api/system/dict/data"; //字典接口
import { listMaWarehouse } from "@/api/smartParks/park/maWarehouse";

import { listUser } from "@/api/system/user";
export default {
  name: "maInOutRecord",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物品出入管理表格数据
      maInOutRecordList: [],
      houseList: [],
      dateRange:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        recordType: 1,
        materialName: null,
        applicant: null,
        enterprise: null,
        phone: null,
        applyTime: null,
        inOutTime: null,
        actualTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      applyType: [],
      materialCategoryList:[],
      userList:[]
    };
  },
  beforeRouteEnter(to, from, next) {
    next(vm=>{
      if (from.name === "PropertyItemsAdd") {
        vm.getList();
      }
    });
  },
  created() {
    this.getList();
    this.getHouseList();
  },
  methods: {
    getHouseList(){
      listMaWarehouse().then((response) => {
        this.houseList = response.rows;
      })
    },
    getType() {
      listData({ dictType: "material_apply_type" }).then((response) => {
        this.applyType = response.rows;
      });
    },
    /** 查询物资类型列表 */
    getdictDataList() {
      listData({ dictType: "material_category" }).then((response) => {
        this.materialCategoryList = response.rows
      });
    },
    getUser(){
      listUser().then((response) => {
        this.userList = response.rows;
      });
    },
    /** 查询物品出入管理列表 */
    async getList() {
      this.loading = true;
      await this.getType();
      await this.getdictDataList();
      await this.getUser();
      listmaInOutRecord(this.addDateRange(this.queryParams, this.dateRange)).then((response) => {
        response.rows.forEach((item) => {
          this.applyType.forEach((itemType) => {
            if (itemType.dictValue === item.applyType) {
              item.typeNew = itemType.dictLabel;
            }
          });
        });
        this.maInOutRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        applyType: null,
        materialName: null,
        userName: null,
        enterprise: null,
        phone: null,
        applyTime: null,
        inOutTime: null,
        actualTime: null,
        createBy: null,
        operateTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd(value) {
      // this.reset();
      // this.open = true;
      // this.title = "物资出库/入库";

      this.$router.push({ name: "PropertyItemsAdd" ,query:{type:value}});
    },
    /** 修改按钮操作 */
    handleUpdate(row,value) {
      this.$router.push({ name: "PropertyItemsAdd",query:{id:row.id,type:value} });
      return
      this.reset();
      const id = row.id || this.ids;
      getmaInOutRecord(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物品出入管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updatemaInOutRecord(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addmaInOutRecord(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除物品出入管理的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delmaInOutRecord(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "park/maInOutRecord/export",
        {
          ...this.queryParams,
        },
        `park_maInOutRecord.xlsx`
      );
    },
  },
};
</script>
